﻿var info = {
    login_name: {
        0: '这是您的登录帐号，需要用邮箱注册',
        err_1: '请使用邮箱作为用户名',
        err_2: '账户名不足6位数，请填写6~20位由字母和数字组成帐号哦',
        err_3: '账户名超出20位数，请填写6~20位由字母和数字组成帐号哦',
        err_4: '该账户名已被占用，推荐 <em id="re_uname"><i>loading...</i></em>'
    },
    login_password: {
        0: '这是您的登录密码，支持 6~20 位字符',
        err_1: '密码不足6位，请填写6~20位字符组成的密码哦',
        err_2: '密码超出20位，请填写6~20位字符组成的密码哦',
        err_3: '密码含有非法字符，请重新填写'
    },
    relogin_pwd: {
        0: '请再次输入密码，确保两次输入的密码一致',
        err_1: '与上面密码不一致，请重新输入'
    },
    email: {
        0: '输入一个你已拥有的邮件地址，用于找回密码、信息验证等',
        err_1: '邮件地址格式有误',
        err_2: '该邮件地址已被注册'
    },
    CompName: {
        0:'请输入您的公司名称'
    },
    Lincense: {
        0: '请输入营业执照号码',
        err_1: '请输入正确的营业执照号'
    },
    ture_name:{
      0:'请输入你的真实姓名'  
    },
    CompAddr:{
        0:'请输入公司地址'
    },
    chk_code: {
        0: '请填写图中的字母或数字，看不清楚可点击“换一个”',
        err_1: '验证码输入错误，请重新输入'
    },
    Mobile: {
        0:'请输入手机号码',
        err_1: '请输入正确的手机号'
    },
     
    empty: '这里不允许为空哦',
    check: '正在检测...'
};
window.info = info;
window.emails_at = ['@qq.com', '@163.com', '@126.com', '@sina.com', '@sohu.com', '@gmail.com', '@139.com', '@hotmail.com', '@yahoo.com.cn', '@sohu.com', '@yahoo.cn'];
var regEx = {
    user_name: /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,5}){1,2})$/,
    auth_code: /^[a-zA-Z0-9_-]$/,
    Lincense: /^[0-9]+$/,
    real_name: /^[\u4e00-\u9fa5]+$/,
    CompAddr: /^[\u4e00-\u9fa5]+$/,
    id_card_num: /(^[0-9]{17}[0-9xX]$)|(^[0-9]{15}$)/,
    email: /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,5}){1,2})$/,
    Mobile: /^1[3|4|5|8][0-9]\d{4,8}$/,
    password: new RegExp("^[\\w\\.(!@#$%&)]+$")
};

// 用户名推荐使用的后缀
var date = new Date();
var this_year = date.getFullYear();
var this_month = date.getMonth() + 1;
var this_day = date.getDate();
window.re_uname_sub = [this_year, this_year - 1, this_year + 1, (this_month < 10 ? '0' + this_month.toString() : this_month.toString()) + (this_day < 10 ? '0' + this_day.toString() : this_day.toString())];
window.re_uname_i = 0;

var code_cache = [];
/* 闪烁 */
function blinkInputBorder(input, c0, c1, times, speed) {
    var is = $(input);
    if (is.length == 0 || times == 0) return;
    var cc = is[0].style.borderColor;
    c0 = cc == c0 ? c1 : c0;
    is.css({ "border-color": c0 });
    times--;
    setTimeout("blinkInputBorder('" + input + "', '" + c0 + "', '" + cc + "', " + times + ", " + speed + ")", speed);
}
function check_id_card(_c) { var _d = new Date(); if (_c.length != 15 && _c.length != 18) { return 0; } if (_c.length == 15) { _c = _c.substr(0, 6) + '19' + _c.substr(6, _c.length - 6); } var _a = (_d.getFullYear() - parseInt(_c.substr(6, 4))); if (_a == 0) { _a = 1; } if (_a < 0 || _a > 150) { return 0; } if (_c.length == 17) { return _a; } var _s = 0; var i = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2); for (k in i) { _s += i[k] * parseInt(_c.substr(k, 1)); } x = new Array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'); k++; return (x[_s % 11] == _c.substr(k, 1)) ? _a : 0; }


$(window).ready(function () {
     
    // 验证码与提示位置互换
    function auth_code_msg(success) {
        var p = $("#chk_code").parent(), msg = p.find('span.msgx'), code = p.find('span.yz_img');
        if (success) {
            code.before(msg);
        } else {
            code.after(msg);
        }
    }
 
    /* 验证码 */
    $(".yz_img img, .yz_img a").click(function () {
        $('#c_code').attr('src', '../control/RandomImg.aspx?id=' + (new Date()).getTime());
        code_cache = [];
        auth_code_msg(false);
        $("#chk_code").focus().val('');
    });
    /*  */
    // 改变提示图标和信息
    function changeMsg(input, i, classNew, txt) {
        var key = input.attr('id'), p = input.parent(), msg = p.find('span.msgx');
        msg.attr('class', classNew);
        if (typeof txt == 'undefined') msg.html(window.info[key][i]);
        else msg.html(txt);
        if (classNew.indexOf('msgC') > -1) input.attr('class', 'txt1 inputB');
        else input.attr('class', 'txt1');
    }
    // 获取可用的用户名
    function getValidUName(user) {
        //获取用户的前半截
        var ustart = user.substr(0, user.indexOf('@'));
        var end = user.substr(user.indexOf('@'), user.length);
        

        if (window.re_uname_i < window.re_uname_sub.length) window.re_uname = ustart + window.re_uname_sub[window.re_uname_i];
        else window.re_uname = ustart + parseInt(Math.random() * 10000)+end;
        window.re_uname_i++; 
        if (window.re_uname_i > 50) $("#re_uname").html('<i>加载错误</i>');
        else
            $.getJSON("../Handler/CommonHandler.ashx?opType=chkuser&PostData=" + window.re_uname + "&temp=" + (Math.round(Math.random() * 10000)), function (result) {
                if (result == false) {
                    $("#re_uname").html('<a href="javascript:">' + window.re_uname+end + '</a>');
                    $("#re_uname a").click(function () {
                        var v = $(this).html(), input = $("#login_name");
                        input.attr("_check", v);
                        input.val(v);
                        changeMsg($("#login_name"), 0, 'msgx msgB', '&nbsp;');
                        $("#login_password").focus();
                    });
                } else {
                    getValidUName($('#login_name').val());
                }
            });

         
    }
    // 检查密码合法性
    function checkPW(pw) {
        if (pw.length < 6) {
            changeMsg($("#login_password"), 'err_1', 'msgx msgC');
            return false;
        }
        if (!regEx.password.test(pw)) {
            changeMsg($("#login_password"), 'err_3', 'msgx msgC');
            return false;
        }
        if (pw.length > 20) {
            changeMsg($("#login_password"), 'err_2', 'msgx msgC');
            return false;
        }
        changeMsg($("#login_password"), 0, 'msgx msgB', '&nbsp;');
        return true;
    }
    // 检查密码强度
    function charMode(iN) {
        if (iN >= 48 && iN <= 57) //数字
            return 1;
        if (iN >= 65 && iN <= 90) //大写字母
            return 2;
        if (iN >= 97 && iN <= 122) //小写
            return 3;
        else  //特殊字符
            return 4;
    }
    function bitTotal(num) {
        modes = 0;
        for (i = 0; i < 4; i++) {
            if (num & 1)
                modes++;
            num >>>= 1;
        }
        return modes;
    }
    function checkStrong(pwd) {
        modes = 0;
        for (i = 0; i < pwd.length; i++) {
            //测试每一个字符的类别并统计一共有多少种模式. 
            modes |= charMode(pwd.charCodeAt(i));
        }
        return bitTotal(modes);
    }

    /* 输入框行为 */
    window.input_focused = false;
    var input_selector = "#login_name, #login_password, #relogin_pwd, #email, #chk_code, #ture_name,#CompName,#Lincense,#CompAddr,#Mobile";
    $(input_selector).mouseover(function () {
        var msg = $(this).parent().find('span.msgx');
        if (!window.input_focused && msg.attr('class').indexOf('msg0') > -1) changeMsg($(this), 0, 'msgx msgE');
    });
    $(input_selector).mouseout(function () {
        var msg = $(this).parent().find('span.msgx');
        if (!window.input_focused && msg.attr('class').indexOf('msgE') > -1) changeMsg($(this), 0, 'msgx msg0', '&nbsp;');
    });
    // 输入框获得焦点
    $(input_selector).focus(function () {
        window.input_focused = true;
        var v = $(this).val(), msg = $(this).parent().find('span.msgx'), id = $(this).attr('id');
        if (id == 'chk_code') $(".yz_img").css({ "display": "inline-block" });
        if (msg.attr('class').indexOf('msg0') > -1 || v == '') {
            changeMsg($(this), 0, 'msgx msgE');
        }
        $(this).attr('class', 'txt1 inputA');
    });
    // 输入框失去焦点
    $(input_selector).blur(function () {
        if ($('#email_auto_pop').is(':visible') && $(this).attr('name') == 'email') {
            return false;
        }
        window.input_focused = false;
        $(this).removeClass('inputA');
        var v = $(this).val(), id = $(this).attr('id');
        if (v == '') { // 不允许留空
            var empty = window.info['empty'];
            if (id == 'ture_name') {
                empty += '，' + window.info['ture_name'][0].substr(0, window.info['ture_name'][0].indexOf('（'));
            }
            if (id == 'sfz') {
                empty = empty + '，' + window.info['sfz'][0].substr(0, window.info['sfz'][0].indexOf('（'));
            }
            changeMsg($(this), 0, 'msgx msgC', empty);
            $(this).attr('_check', '');
            return;
        }
        var p = $(this).parent(), msg = p.find('span.msgx'), _c = $(this).attr('_check');
       
        switch (id) {
            case 'login_name': // 检查用户名
                if (!regEx.user_name.test(v)) {
                  
                    changeMsg($(this), 'err_1', 'msgx msgC'); 
                } else {
                    if (_c != v) {
                        changeMsg($(this), 0, 'msgx msgD', window.info['check']);
                        $.getJSON("../handler/commonHandler.ashx?OpType=chkuser&postdata=" + v, function (result) {
                           // alert(result);
                            if (result == true) {
                                changeMsg($('#login_name'), 'err_4', 'msgx msgC');
                                window.re_uname_i = 0;
                                getValidUName(v);
                            } else {

                                changeMsg($('#login_name'), 0, 'msgx msgB', '&nbsp;');
                              
                            }
                        });
                       
                    }
                }
                $(this).attr('_check', v);
                break;
            case 'login_password': // 检查密码
                if (checkPW(v)) $("#relogin_pwd").blur().focus();
                break;
            case 'relogin_pwd': // 检查密码重输
                if (v == $("#login_password").val()) {
                    changeMsg($(this), 0, 'msgx msgB', '&nbsp;');
                } else {
                    changeMsg($(this), 'err_1', 'msgx msgC');
                }
                break;
            case 'chk_code': // 检查验证码
                if (!regEx.auth_code.test(v)) {
                    
                        $.getJSON("../Handler/CommonHandler.ashx?opType=chkcode&PostData=" + v + "&temp=" + (Math.round(Math.random() * 10000)) , function (result) {
                            code_cache[v] = result;
                           
                            if (result == true) {
                                changeMsg($("#chk_code"), 0, 'msgx msgB', '&nbsp;');
                                auth_code_msg(true);
                                 
                            } else {
                                changeMsg($("#chk_code"), 'err_1', 'msgx msgC');
                                auth_code_msg(false);
                            }
                        });
                        
                        changeMsg($(this), 0, 'msgx msgD', window.info['check']);
                     
                } else {
                    changeMsg($(this), 'err_1', 'msgx msgC');
                    auth_code_msg(false);
                }
                $(this).attr('_check', v);
                break;
            case 'ture_name': // 检查真实姓名
                if (regEx.real_name.test(v)) {
                    changeMsg($(this), 0, 'msgx msgB', '&nbsp;');
                } else {
                    changeMsg($(this), 'err_1', 'msgx msgC');
                }
                break;
            case 'CompAddr': // 检查公司地址
                if (regEx.real_name.test(v)) {
                    changeMsg($(this), 0, 'msgx msgB', '&nbsp;');
                } else {
                    changeMsg($(this), 'err_1', 'msgx msgC');
                }
                break;
            case 'CompName':
                if (regEx.real_name.test(v)) {
                    changeMsg($(this), 0, 'msgx msgB', '&nbsp;');
                } else {
                    changeMsg($(this), 'err_1', 'msgx msgC');
                }
                break;
            case 'Lincense':
                
                if (regEx.Lincense.test(v)) {
                    changeMsg($(this), 0, 'msgx msgB', '&nbsp;');
                } else {
                    changeMsg($(this), 'err_1', 'msgx msgC');
                }
                break;
            case 'sfz': // 检查身份证号码
                if (regEx.id_card_num.test(v)) {
                    if (_c != v) {
                        changeMsg($(this), 0, 'msgx msgD', window.info['check']);
                       
                        var id_card_check = check_id_card(v.toUpperCase());
                        if (id_card_check >= 18) changeMsg($("#sfz"), 0, 'msgx msgB', '&nbsp;');
                        else if (id_card_check > 0) changeMsg($("#sfz"), 0, 'msgx msgB', '为未成年人');
                        else changeMsg($("#sfz"), 'err_1', 'msgx msgC');
                    }
                } else {
                    changeMsg($(this), 'err_1', 'msgx msgC');
                }
                $(this).attr('_check', v);
                break;
            case 'Mobile':
                
                if (!regEx.Mobile.test(v)) {
                    changeMsg($(this), 'err_1', 'msgx msgC');
                } else {
                    changeMsg($(this), 0, 'msgx msgB', '&nbsp;');
                }
                break;
        }
    });
    /*  */
    $("#login_password").keyup(function () {
        var v = $(this).val(), p = $(this).parent().find('span.Monitoring');
        checkPW(v);
        if (v.length > 5 && regEx.password.test(v)) {
            p.css({ "display": "inline-block" });
            var _s = checkStrong(v);
            if (_s < 1 || _s > 3) _s = 0;
            $('#pwlevel').attr('class', 'Strength' + _s);
        } else {
            p.hide();
        }
    });
    $("#agree").bind('change click', function () {
        if ($(this)[0].checked) {
            changeMsg($(this), 0, 'msgx msgB', '&nbsp;');
        } else {
            changeMsg($(this), 'err_1', 'msgx msgC');
        }
        $(this).removeAttr('class');
    });
    // 自动完成邮箱推荐
    $('#login_name').attr('autocomplete', 'off');
    $('#login_name').bind('keyup focus', function (event) {
        if ($(this).val() == '') {
            $('#email_auto_pop').hide();
            return;
        }

        if (event.keyCode == 38) {//上
            _this_span = $('#email_auto_pop span.on').removeClass('on');
            if (typeof _this_span.prev().attr('tagName') != 'undefined') {
                _this_span.prev().addClass('on');
            } else {
                $('#email_auto_pop span:last').addClass('on');
            }
            return;
        }
        if (event.keyCode == 40) {//下
            _this_span = $('#email_auto_pop span.on').removeClass('on');
            if (typeof _this_span.next().attr('tagName') != 'undefined') {
                _this_span.next().addClass('on');
            } else {
                $('#email_auto_pop span:first').addClass('on');
            }
            return;
        }

        _emails = window.emails_at;
        if (typeof ($('#email_auto_pop').attr('tagName')) == 'undefined') {
            $('body').append('<div id="email_auto_pop"></div>');
        }

        $('#email_auto_pop').show();

        _list = '';

        _this_val = $(this).val().split('@');
        _mail_domain = typeof _this_val[1] != 'undefined' ? _this_val[1] : '';
        _this_val = _this_val[0];
        for (i in _emails) {
            if (_mail_domain != '') {
                if (_emails[i].indexOf(_mail_domain) == -1) {
                    continue;
                }
            }
            _list += '<span>' + _this_val + _emails[i] + '</span>';
        }
        if (_list == '') {
            $('#email_auto_pop').hide();
            return;
        }
        $('#email_auto_pop').html(_list);

        if ($(this).val().length < 17) {
            $('#email_auto_pop').width($(this).width());
        } else {
            $('#email_auto_pop').width($(this).width() * 2);
        }
        $('#email_auto_pop').css({
            'left': $(this).offset().left,
            'top': $(this).offset().top + $(this).height()
        });
        $('#email_auto_pop span').unbind();
        $('#email_auto_pop span:first').addClass('on');
        $('#email_auto_pop span').mouseover(function () {
            $('#email_auto_pop span').removeClass('on');
            $(this).addClass('on');
        }).click(function () { // 点击
            $('#login_name').val($('#email_auto_pop span.on').text());
            $('#email_auto_pop').hide();
            $('#login_name').trigger('blur');
            $('#chk_code').focus();
        });

    }).keydown(function (e) {
        if (e.keyCode == 13) {
            $(this).val($('#email_auto_pop span.on').text());
            $('#email_auto_pop').hide();
            window.email_enter = true;
            return false;
        }
    }).blur(function () {
        $(this).removeClass('inputA');
        if (typeof ($('#email_auto_pop').attr('tagName')) != 'undefined') {
            setTimeout(function () {
                $('#email_auto_pop').hide();
            }, 250);
        }
        var _c = $(this).attr('_check'), v = $(this).val();
        if (v == '') {
            changeMsg($(this), 0, 'msgx msgC', window.info['empty']);
        } else if (regEx.email.test(v)) {
            if (_c != v) {
                changeMsg($(this), 0, 'msgx msgD', window.info['check']);
                $.getJSON("../Handler/CommonHandler.ashx?opType=chkcode&PostData=" + v + "&temp=" + (Math.round(Math.random() * 10000)) + "&jsoncallback=?", function (result) {
                    if (result == 1) {
                        changeMsg($("#login_name"), 0, 'msgx msgB', '&nbsp;');
                    } else {
                        changeMsg($("#login_name"), 'err_2', 'msgx msgC');
                    }
                });

              
            }
        } else {
            changeMsg($(this), 'err_1', 'msgx msgC');
        }
        $(this).attr('_check', v);
        if (window.email_enter) {
            $("#chk_code").focus();
            window.email_enter = false;
        }
    });
    /*  */
    $("#aspnetForm").submit(function () {
        window.input_ok = true;
        $(input_selector).each(function () {
            if ($(this).parent().find('span.msgx').attr('class').indexOf('msgB') == -1) {
                window.input_ok = false;
                $(this).blur();
            }
        });
       
        $(".inputB:eq(0)").focus();
        blinkInputBorder(".inputB", "orange", "", 4, 180);
        

        return window.input_ok;
    });
    /*  */
    $("#login_name").focus();
  
    if ($(".tit").length) $(window).scrollTop($(".tit").offset().top);
    $("#reset_all").click(function () {
        $(".Monitoring").hide();
        $(".yz_img").hide();
        $(".txt1").each(function () {
            $(this).val('');
            changeMsg($(this), 0, 'msgx msg0', '&nbsp;');
        });
        auth_code_msg(false);
        $("#login_name").focus();
    });
});
